package com.tpc.client.modules.core.document;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.HoverCustomizer;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.tpc.client.modules.core.document.datasource.DocumentListDS;
import com.tpc.client.systeme.core.Systeme;
import com.tpc.client.systeme.core.TPCT_MODULE;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2010-08-15
 */

public class DocumentUi implements TPCT_MODULE {

    Systeme systeme;

    String MODULEID = "DOCUMENTS";
    
    Canvas cnvEmpty = new Canvas();
   
    ListGrid GRID_LIST = new ListGrid();
    
    DocumentListDS DOCUMENT_LIST_DS = new DocumentListDS("DOCUMENT_LIST_DS");

    Boolean isUiLoaded = false;
    Boolean isListBuild = false;
    Boolean isListFetch = false;

    public DocumentUi() {
        
    }

    public Canvas loadUI(Systeme systemeParam,String MODULEIDp) {
        if (!isUiLoaded) {
            systeme = systemeParam;
            MODULEID = MODULEIDp;
            cnvEmpty = systeme.getTabpaneTemplate(cnvEmpty);
            cnvEmpty.setID("CANVAS_"+MODULEID);
            cnvEmpty.addChild(getList());
            loadList();
            isUiLoaded = true;
        }
        return cnvEmpty;
    }

    // compatible to implement TPC_MODULE
    public void create(ListGridRecord record) {}
    public DynamicForm showAndCreate(ListGridRecord record) { return null; }
    public void buildForm() {}
    public void loadForm(ListGridRecord record) {}
    public DynamicForm getForm() { return null; }
    
    public void buildList() {
        if (!isListBuild) {
            GRID_LIST = systeme.getGridTemplate(GRID_LIST);
            
            DOCUMENT_LIST_DS.setObjects(systeme,MODULEID);
            GRID_LIST.setDataSource(DOCUMENT_LIST_DS);

            ListGridField fldDocumentId = new ListGridField("document_liste_id");
            fldDocumentId.setHidden(Boolean.TRUE);
            ListGridField fldDocumentTypeIcon = new ListGridField("document_liste_type_icon"," ",30);
            fldDocumentTypeIcon.setAlign(Alignment.CENTER);
            fldDocumentTypeIcon.setType(ListGridFieldType.IMAGE);
            fldDocumentTypeIcon.setImageURLPrefix("");
            fldDocumentTypeIcon.setImageURLSuffix(".png");
            ListGridField fldDocumentName = new ListGridField("document_liste_name", "Nom");
            fldDocumentName.setHoverCustomizer(new HoverCustomizer() {
                public String hoverHTML(Object value, ListGridRecord record, int rowNum, int colNum) {
                    String strHover = "<table width=200><tr><td valign=top>notes: </td><td>"+record.getAttribute("document_liste_notes")+"</td></tr></table>";
                    return strHover;
                }
            });
            ListGridField fldDocumentDescription = new ListGridField("document_liste_notes");
            fldDocumentDescription.setHidden(Boolean.TRUE);
            ListGridField fldDocumentPath = new ListGridField("document_liste_path");
            fldDocumentPath.setHidden(Boolean.TRUE);
            ListGridField fldDocumentSize = new ListGridField("document_liste_size","Taille",80);
            ListGridField fldDocumentType = new ListGridField("document_liste_type","Type",80);
            ListGridField fldDocumentUploaded = new ListGridField("document_liste_uploaded","Telecharge",110);
            ListGridField fldDocumentDownloadIcon = new ListGridField("document_liste_download_icon"," ",30);
            fldDocumentDownloadIcon.setAlign(Alignment.CENTER);
            fldDocumentDownloadIcon.setType(ListGridFieldType.IMAGE);
            fldDocumentDownloadIcon.setImageURLPrefix("");
            fldDocumentDownloadIcon.setImageURLSuffix(".png");

            GRID_LIST.setFields(fldDocumentId,fldDocumentTypeIcon,fldDocumentName,fldDocumentDescription,fldDocumentSize,fldDocumentType,fldDocumentUploaded,fldDocumentDownloadIcon,fldDocumentPath);

            GRID_LIST.addCellClickHandler(new CellClickHandler() {
                public void onCellClick(CellClickEvent event) {
                    systeme.startLoading();
                    ListGridRecord record =  event.getRecord();
                    int colNum = event.getColNum();
                    String fieldName = GRID_LIST.getFieldName(colNum);
                    if (fieldName.equals("document_liste_download_icon")) {
                        String strUrl = record.getAttribute("document_liste_path")+record.getAttribute("document_liste_name");
                        if (strUrl != null) {
GWT.log("path:"+strUrl);                            
                            com.google.gwt.user.client.Window.open(strUrl,"new_download_window",null);
                        }
                        else {
                            SC.say("Fichier invalide.");
                            systeme.stopLoading();
                        }
                    }
                    else {
                        SC.say("Fichier invalide.");
                        systeme.stopLoading();
                    }
                    systeme.stopLoading();
                }
            });
/*
            GRID_LIST.addCellDoubleClickHandler(new CellDoubleClickHandler() {
                public void onCellDoubleClick(CellDoubleClickEvent event) {
                    systeme.startLoading();
                    final ListGridRecord record = event.getRecord();
                    loadForm(record);
                }
            });

            GRID_LIST.addFilterEditorSubmitHandler(new FilterEditorSubmitHandler() {
                public void onFilterEditorSubmit(FilterEditorSubmitEvent event) {
                    ResultSet rs = GRID_LIST.getResultSet();
                    rs.addDataArrivedHandler(new DataArrivedHandler() {
                        public void onDataArrived(DataArrivedEvent event) {
                            systeme.getAppBar().getTab(MODULEID).setTitle(systeme.getDictionary().MODULE_DOCUMENT()+" ("+GRID_LIST.getResultSet().getLength()+")");
                        }
                    });
                }
            });
*/
        }
        isListBuild = true;
    }

    public ListGrid getList() {
        if (!isListBuild) { buildList(); }
        return GRID_LIST;
    }

    public void loadList() {
        if (!isListBuild) { buildList(); }
        if (!isListFetch) {
            GRID_LIST.invalidateCache();
            GRID_LIST.fetchData();
            isListFetch = true;
        }
        else {
            systeme.stopLoading();
        }
    }

}